<template>
  <div id="aCoursesList" class="bg-fa of">
    <!-- 课程展示 -->
    <section class="container">
      <header class="comm-title">
        <h2 class="fl tac">
          <span class="c-333">全部课程</span>
        </h2>
      </header>
      <section class="c-sort-box">
        <section class="c-s-dl">
          <dl>
            <dt>
              <span class="c-999 fsize14">所属科目</span>
            </dt>
            <dd class="c-s-dl-li">
              <!-- 一级科目 -->
              <ul class="clearfix">
                <li :class="{current:!$route.query.subjectParentId}">
                  <a
                    title="全部"
                    href="javascript:void(0);"
                    @click="searchPrimarySubject('')"
                  >
                    全部
                  </a>
                </li>
                <li
                  v-for="item in subjectList"
                  :key="item.id"
                  :class="{current:$route.query.subjectParentId===item.id}">
                  <a
                    :title="item.title"
                    href="javascript:void(0);"
                    @click="searchPrimarySubject(item.id)"
                  >
                    {{ item.title }}
                  </a>
                </li>
              </ul>
            </dd>
          </dl>
          <dl>
            <dt>
              <span class="c-999 fsize14"/>
            </dt>
            <dd class="c-s-dl-li">
              <!-- 二级科目 -->
              <ul v-if="$route.query.subjectParentId" class="clearfix">
                <li :class="{current:!$route.query.subjectId}">
                  <a
                    title="全部"
                    href="javascript:void(0);"
                    @click="searchSecondarySubject('')"
                  >
                    全部
                  </a>
                </li>
                <li
                  v-for="item in secondarySubjectList"
                  :key="item.id"
                  :class="{current:$route.query.subjectId===item.id}">
                  <a
                    :title="item.title"
                    href="javascript:void(0);"
                    @click="searchSecondarySubject(item.id)"
                  >
                    {{ item.title }}
                  </a>
                </li>
              </ul>
            </dd>
          </dl>
          <div class="clear"/>
        </section>
        <div class="js-wrap">
          <section class="fl">
            <!-- 排序 -->
            <ol class="js-tap clearfix">
              <li :class="{'current bg-green': $route.query.buyCountSort}">
                <a title="销量" href="javascript:void(0);" @click="searchBuyCount()">
                  <svg-icon icon-class="top" style="vertical-align: -0.15em;margin-right: 2px"/>
                  销量最高
                </a>
              </li>
              <li :class="{'current bg-green': $route.query.gmtCreateSort}">
                <a title="最新" href="javascript:void(0);" @click="searchGmtCreate()">
                  <svg-icon icon-class="fire" style="vertical-align: -0.15em;margin-right: 2px"/>
                  最新发布
                </a>
              </li>
              <li :class="{'current bg-green': $route.query.priceSort}">
                <a v-if="!$route.query.type || $route.query.type == 1" title="价格" href="javascript:void(0);"
                   @click="searchPrice(2)">
                  价格
                  <svg-icon icon-class="up-arrow" style="vertical-align: -0.15em;"/>
                </a>
                <a v-if="$route.query.type == 2" title="价格" href="javascript:void(0);" @click="searchPrice(1)">
                  价格
                  <svg-icon icon-class="down-arrow" style="vertical-align: -0.15em;"/>
                </a>
              </li>
            </ol>
          </section>
        </div>
        <div class="mt40">
          <!-- 无数据提示 -->
          <section v-if="courseList.length===0" class="no-data-wrap">
            <span class="c-666 fsize14 ml10 vam">暂时没有相关数据</span>
          </section>
          <!-- 数据列表 -->
          <article v-if="courseList.length>0" class="comm-course-list">
            <ul id="bna" class="of">
              <li v-for="item in courseList" :key="item.id">
                <div class="cc-l-wrap">
                  <section class="course-img">
                    <!-- 课程封面 -->
                    <img :src="item.cover" :alt="item.title" class="img-responsive">
                    <div class="cc-mask">
                      <a :href="'/course/'+item.id" title="开始学习" class="comm-btn c-btn-1">开始学习</a>
                    </div>
                  </section>
                  <h3 class="hLh30 txtOf mt10">
                    <!-- 课程标题 -->
                    <a :href="'/course/'+item.id" :title="item.title" class="course-title fsize18 c-333">
                      {{ item.title }}
                    </a>
                  </h3>
                  <section class="mt10 hLh20 of">
                    <!-- 课程价格 -->
                    <span v-if="Number(item.price) === 0" class="fr jgTag bg-green">
                      <i class="c-fff f-fM">限时免费</i>
                    </span>
                    <span v-else class="fr jgTag ">
                      <i class="c-red f-fM"> ￥{{ item.price }}</i>
                    </span>
                    <!-- 课程人次 -->
                    <span class="fl jgAttr c-ccc f-fM">
                      <i class="c-999 f-fM">
                        <svg-icon icon-class="eye" style="vertical-align: -0.15em;"/>
                        {{ item.viewCount }}人浏览
                      </i>
                      |
                      <i class="c-999 f-fM">
                        <svg-icon icon-class="money" style="vertical-align: -0.15em;"/>
                        {{ item.buyCount }}人购买
                      </i>
                    </span>
                  </section>
                </div>
              </li>
            </ul>
            <div class="clear"/>
          </article>
        </div>
      </section>
    </section>
  </div>
</template>

<script>
import courseApi from '~/api/course'
import subjectApi from '~/api/subject'
import querystring from 'querystring' // url参数拼接工具

export default {
  async asyncData(page) {
    /*
     * 组装查询参数
     * 当点击查询条件的时候, 在url地址栏中组装查询参数
     * 从url地址栏中获取查询参数, 对页面中响应部分的内容进行高亮显示
     */
    const searchObj = {}
    // 从url中组装查询参数
    const query = page.route.query
    searchObj.courseTitle = query.courseTitle || ''
    searchObj.subjectParentId = query.subjectParentId || ''
    searchObj.subjectId = query.subjectId || ''
    searchObj.buyCountSort = query.buyCountSort || ''
    searchObj.gmtCreateSort = query.gmtCreateSort || ''
    searchObj.priceSort = query.priceSort || ''
    searchObj.type = query.type || '' // 1正序,2倒序
    // 获取课程列表
    const courseListResponse = await courseApi.queryBy(searchObj)
    const courseList = courseListResponse.data.courseList

    // 获取一级科目列表
    const subjectListResponse = await subjectApi.getTreeList()
    const subjectList = subjectListResponse.data.items
    // 遍历一级科目列表 来获取二级科目列表
    let secondarySubjectList = []
    subjectList.forEach(subject => {
      if (subject.id === searchObj.subjectParentId) {
        // 组装当前一级科目下的二级科目数据
        secondarySubjectList = subject.children
      }
    })
    return {
      courseList, // 课程列表
      subjectList, // 一级科目列表
      secondarySubjectList, // 二级科目列表
      searchObj // 查询对象
    }
  },
  methods: {
    // 选择一级科目
    searchPrimarySubject(subjectParentId) {
      // 让页面根据新的url地址刷新
     // window.location = 'course?subjectParentId=' + subjectParentId
      const queryObj = {
        courseTitle: this.searchObj.courseTitle,
        subjectParentId: subjectParentId
      }
      const queryStr = querystring.stringify(queryObj)
      window.location = 'course?' + queryStr
    },
    // 选择二级科目
    searchSecondarySubject(subjectId) {
      // window.location = 'course?subjectId=' + subjectId + '&subjectParentId=' + this.searchObj.subjectParentId
      const queryObj = {
        courseTitle: this.searchObj.courseTitle,
        subjectId: subjectId,
        subjectParentId: this.searchObj.subjectParentId
      }
      const queryStr = querystring.stringify(queryObj)
      window.location = 'course?' + queryStr
    },
    // 选择按销量倒序
    searchBuyCount() {
      // window.location = 'course?buyCountSort=1' + '&subjectId=' + this.searchObj.subjectId + '&subjectParentId=' + this.searchObj.subjectParentId
      const queryObj = {
        courseTitle: this.searchObj.courseTitle,
        subjectId: this.searchObj.subjectId,
        subjectParentId: this.searchObj.subjectParentId,
        buyCountSort: 1
      }
      const querys = querystring.stringify(queryObj)
      window.location = 'course?' + querys
    },
    // 选择按创建时间正序
    searchGmtCreate() {
      const queryObj = {
        courseTitle: this.searchObj.courseTitle,
        subjectId: this.searchObj.subjectId,
        subjectParentId: this.searchObj.subjectParentId,
        gmtCreateSort: 1
      }
      const querys = querystring.stringify(queryObj)
      window.location = 'course?' + querys
    },
    // 按价格排序
    searchPrice(type) {
      const queryObj = {
        courseTitle: this.searchObj.courseTitle,
        subjectId: this.searchObj.subjectId,
        subjectParentId: this.searchObj.subjectParentId,
        priceSort: 1,
        type: type
      }
      const querys = querystring.stringify(queryObj)
      window.location = 'course?' + querys
    }
  }
}
</script>
